package references

import "jvmgo/ch10/instructions/base"
import "jvmgo/ch10/rtda"
import "jvmgo/ch10/rtda/heap"

// 检查类是否是指定类型（不改变操作数栈，判断失败抛出异常）
type CHECK_CAST struct {
    base.Index16Instruction
}

func (self *CHECK_CAST) Execute(frame *rtda.Frame) {
    stack := frame.OperandStack()
    ref := stack.PopRef()
    stack.PushRef(ref)
    if ref == nil {
        return
    }

    cp := frame.Method().Class().ConstantPool()
    classRef := cp.GetConstant(self.Index).(*heap.ClassRef)
    class := classRef.ResolvedClass()
    if !ref.IsInstanceOf(class) {
        panic("java.lang.ClassCastException")
    }
}
